﻿class game.TimeShower {
	var hour_txt:MovieClip;
	var minute_txt:MovieClip;
	var second_txt:MovieClip;
	var t;
	var hours;
	var minutes;
	var seconds;
	var over;
	var endTime;
	function TimeShower(ctt:MovieClip) {
		if (ctt.hour_txt.constructor != TextField) {
			throw new Error('!!![TimeShower]  hour_txt');
		}
		if (ctt.minute_txt.constructor != TextField) {
			throw new Error('!!![TimeShower]  minute_txt');
		}
		if (ctt.second_txt.constructor != TextField) {
			throw new Error('!!![TimeShower]  second_txt');
		}
		this.hour_txt = ctt.hour_txt;
		this.minute_txt = ctt.minute_txt;
		this.second_txt = ctt.second_txt;
	}
	function start(over:Function) {
		this.over = over;
		t = 0;
		_root.tttt = setInterval(this, 'onTimer', 1000);
	}
	function onTimer() {
		var goTime = t;
		hours = int(goTime/3600);
		minutes = int((goTime/3600-hours)*60);
		seconds = goTime%60;
		this.hour_txt.text = formatTime(this.hours, true);
		this.minute_txt.text = formatTime(this.minutes, true);
		this.second_txt.text = formatTime(this.seconds, false);
		if (goTime>endTime) {
			stop();
		}
		t++;
	}
	function stop() {
		clearInterval(_root.tttt);
		over();
	}
	function dispose() {
		clearInterval(_root.tttt);
	}
	function formatTime(n:Number, hasSep:Boolean) {
		var str:String = "";
		if (n<10) {
			str = "0"+n;
		} else {
			str = n.toString();
		}
		if (hasSep) {
			str += ":";
		}
		return str;
	}
}
